<template>
  <div id="testlist">
    <el-dialog
      :title="dialogtitle"
      :visible.sync="porjectEidtFormVisible"
      width="98%" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <projectEdit @dialogHide="saveSuccess" :projectId="projectId" :readOnly="readonly" :timestamp="timestamp" :showUnit="showUnit"></projectEdit>
    </el-dialog>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item>
      <el-breadcrumb-item>资料下发</el-breadcrumb-item>
    </el-breadcrumb>
    <section class="wboxs">
    <!--工具条-->
        <el-form :inline="true" :model="filters">
          <div>
            <!-- <el-form-item label="年度">
              <el-select v-model="filters.theyear" placeholder="请选择" style="width:100px">
                <el-option label="全部" value=""></el-option>
                <el-option v-for="(item, index) in yearlist" :key="index" :label="item" :value="item"></el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item label="">
              <el-input v-model="filters.key" placeholder="关键字" @keyup.enter.native="searchPrj"></el-input>
            </el-form-item>
            <!-- <el-form-item label="状态">
              <el-select v-model="filters.status" placeholder="请选择" style="width:100px">
                <el-option label="全部" value=""></el-option>
                <el-option v-for="(item, index) in statusdic" :key="index" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item>
              <el-button type="primary" @click="searchPrj" icon="el-icon-search">查询</el-button>
            </el-form-item>
          </div>
        </el-form>
    </section>
    <section class="wboxd" style="margin-top:10px">
      <!--列表-->
      <h1>通知列表</h1>
      <el-table :data="prjs" highlight-current-row v-loading="listLoading" style="width: 100%;" @sort-change='sortChange' :height="tableHeight" border>
        <el-table-column type="index" label=" " width="50" fixed align="center" class-name="tableth">
        </el-table-column>
        <el-table-column prop="title" label="标题" show-overflow-tooltip>
        </el-table-column>
        <!-- <el-table-column prop="createUsername" label="创建人" width="100" show-overflow-tooltip>
        </el-table-column> -->
        <el-table-column label="发布时间" width="170" prop="publish_date">
          <template slot-scope="scope">
            {{scope.row.publishDate}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <el-button size="small" @click="detailPrj(scope.row.id)" type="text" icon="el-icon-search">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    <div class="block">
      <el-pagination :total="totalnum" :page-size="pageSize" @current-change="handleCurrentChange" :current-page="pageNum" @size-change="handleSizeChange" :page-sizes="[10, 20, 30, 50]" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    </div>
    </section>
  </div>
</template>

<script>
import getunits from '@/js/common.js'
import projectEdit from './detail'
export default {
  components: {
    projectEdit
  },
  data () {
    return {
      showUnit: false,
      readonly: true,
      prjs: [],
      projectId: null,
      dialogtitle: '新建通知',
      timestamp: '',
      porjectEidtFormVisible: false,
      porjectEidtFormLoading: false,
      porjectEidtForm: {
        beginDate: '',
        endDate: '',
        fileId: '',
        projectId: '',
        title: '',
        unitIds: []
      },
      filters: {
        key: ''
      },
      total: 0,
      pageNum: 1,
      pageSize: 20,
      listLoading: false,
      totalnum: 0,
      Isace: true,
      Sortkey: '',
      changeSizeFlag: false,
      tableHeight: 100,
      statusdic: [],
      myloading: false,
      myname: ''
    }
  },
  methods: {
    setState (id, state, title) {
      const stateText = state ? ' 启用 ' : ' 停用 '
      this.$confirm('确定要' + stateText + ' “' + title + '” 这篇通知吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http
          .post(this.$http.defaults.fileDistributionAPI + 'api/busArticle/save', { id: id, enabled: state })
          .then(response => {
            if (response.data.success === true) {
              this.$message({
                message: stateText + '成功',
                type: 'success'
              })
              this.getList()
            } else {
              console.log(stateText + '失败:' + response.data.message)
              this.$message({
                message: stateText + '失败:' + response.data.message,
                type: 'error'
              })
            }
          })
          .catch(error => {
            console.log(error)
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已放弃' + stateText + '操作'
        })
      })
    },
    saveSuccess () {
      this.porjectEidtFormVisible = false
      this.getPrj()
    },
    addnew () {
      this.projectId = 0
      this.readonly = false
      this.timestamp = new Date().getTime()
      this.porjectEidtFormVisible = true
    },
    detailPrj (id) {
      this.dialogtitle = '查看详情'
      this.projectId = id
      this.readonly = true
      this.timestamp = new Date().getTime()
      this.porjectEidtFormVisible = true
    },
    showForm (id) {
      this.preview = true
      this.projectId = id
      this.readonly = false
      this.timestamp = new Date().getTime()
      this.formShowVisible = true
    },
    editPrj (id) {
      this.dialogtitle = '编辑通知'
      this.projectId = id
      this.readonly = false
      this.timestamp = new Date().getTime()
      this.porjectEidtFormVisible = true
    },
    handleSizeChange (val) {
      this.changeSizeFlag = true
      this.pageNum = 1
      this.pageSize = val
      this.getPrj()
    },
    handleCurrentChange (val) {
      if (this.changeSizeFlag === false) {
        this.pageNum = val
        this.getPrj()
      }
    },
    querySearch (queryString, cb) {
      return getunits(this, queryString, cb)
    },
    // 查询
    searchPrj () {
      this.pageNum = 1
      this.getPrj()
    },
    // 排序
    sortChange: function (column, prop, order) {
      this.Sortkey = column.prop
      if (column.order === 'ascending') {
        this.Isace = true
      } else {
        this.Isace = false
      }
      this.getPrj()
    },
    getPrj () {
      let para = {
        key: this.filters.key,
        orderField: this.Sortkey,
        orderType: this.Isace,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }
      this.listLoading = true
      // var personParams = { Page: 0, Pagenum: 20, Personname: '', Unitkey: '', Sortkey: '', Isace: true, timestamp: new Date().getTime() }
      this.$http
        .post(this.$http.defaults.fileDistributionAPI + `api/busArticle/queryByPage`, para)
        .then(response => {
          this.listLoading = false
          this.prjs = response.data.result.records
          this.totalnum = response.data.result.total
          this.changeSizeFlag = false
        })
        .catch(error => {
          this.$message({
            message: '获取失败:' + error,
            type: 'error'
          })
        })
    },
    delPrj (id) {
      this.$confirm('确认删除该项目吗?', '提示', {
        type: 'error'
      }).then(() => {
        let para = {ids: [id]}
        this.$http.post(this.$http.defaults.fileDistributionAPI + `api/busArticle/remove`, para).then(response => {
          if (response.data.success === true) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          } else {
            console.log('删除失败:' + response.data.message)
            this.$message({
              message: '删除失败:' + response.data.message,
              type: 'error'
            })
          }
          this.getPrj()
        })
      }).catch(() => {
      })
    },
    tableresize (obj) {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      }
      obj.$nextTick(function () {
        obj.tableHeight = clientHeight - 292
      })
    },
    // getDics () {
    //   var _this = this
    //   this.$http
    //     .get(`/api/prj/dic`)
    //     .then(function (response) {
    //       _this.statusdic = response.data.statusdic
    //       _this.yearlist = response.data.yeardic
    //     })
    //     .catch(function (error) {
    //       console.log(error)
    //       _this.$message({
    //         message: '网络获取失败:' + error,
    //         type: 'error'
    //       })
    //     })
    // },
    stateText (id) {
      switch (id) {
        case 0:
          return '未启动'
        case 1:
          return '已启动'
        case 2:
          return '<i class="el-icon-success" style="color:#67C23A"></i> 已结束'
      }
    }
  },
  mounted () {
    this.myname = localStorage.getItem('userTrueName')
    this.getPrj()
    this.tableresize(this)
    let _this = this
    window.onresize = function windowResize () {
      // 通过捕获系统的onresize事件触发我们需要执行的事件
      _this.tableresize(_this)
    }
  }
}
</script>
<style>
  .wboxd .el-table th{
    font-weight: normal;
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
  }
  .wboxd .el-table td, .el-table th{
    padding:0;
    font-size:var(--size14) !important;
    line-height: 20px;
    font-family: '宋体'
  }
  .tableth{
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    height:33px;
  }
  #testlist .wboxs{
    height:45px;
    padding-top:10px;
  }
  .el-table--border th, .el-table--border td {
    border-right: 1px solid #95B8E7;
  }
  .el-table th.is-leaf, .el-table td {
    border-bottom: 1px solid #95B8E7;
  }

</style>
<style scoped>
  h1{
    font-size:var(--size16) !important;
    font-weight: bolder;
    height: 30px;
    border-bottom: 1px solid #95B8E7;
    font-family: '宋体';
    line-height: 30px;
    color: #000;
    padding-left:10px;
    background-image: linear-gradient(to bottom , #EEF4FF, #E0ECFF);
    margin:0;
  }
  .el-pagination{
    float: left;
    padding:7px 10px;
  }
  .wboxs{
    padding:5px 15px;
    width: calc(100% - 30px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
  .wboxd{
    padding:0;
    width: calc(100% - 0px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
</style>
